package cn.itsource.pethome.system.shop.service.impl;

import cn.itsource.pethome.constants.BaseConstants;
import cn.itsource.pethome.system.shop.domain.Shop;
import cn.itsource.pethome.system.shop.domain.ShopRo;
import cn.itsource.pethome.system.shop.mapper.ShopMapper;
import cn.itsource.pethome.system.shop.query.ShopQuery;
import cn.itsource.pethome.system.shop.service.IShopService;
import cn.itsource.pethome.base.service.impl.IBaseServiceImpl;
import cn.itsource.pethome.system.user.domain.Employee;
import cn.itsource.pethome.system.user.mapper.EmployeeMapper;
import cn.itsource.pethome.utils.AuthService;
import cn.itsource.pethome.utils.BusinessException;
import cn.itsource.pethome.utils.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xlh
 * @since 2023-01-07
 */
@Service
public class ShopServiceImpl extends IBaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private  AuthService authService;

    @Override
    public void add(Shop shop) {
        if (shop.getName() == null){
            throw  new BusinessException("店铺名不能为空");
        }
        if (shop.getTel() == null){
            throw  new BusinessException("联系方式不能为空");
        }
        if (shop.getAddress() == null){
            throw  new BusinessException("地址不能为空");
        }
        //百度AI
        if(!authService.verification(shop.getName())){
            throw  new BusinessException("你的店铺存在违规信息");
        }


        /*效验店铺名是否存在*/
        ShopQuery shopQuery = new ShopQuery();
        /*前端传来的店铺名字设置到我们自己的name里面 */
        shopQuery.setName(shop.getName());
        /*数据库根据name查询有几条*/
        Long count = shopMapper.queryCount(shopQuery);
        /*如果数据库查到前端用户输入的店铺名 ！=0 就抛异常*/
        if (count != 0){
            throw  new BusinessException("该["+shop.getName()+"]店铺名已存在");
        }


        /*补充默认数据*/
        shop.setRegisterTime(new Date());//注册时间
        shop.setState(BaseConstants.Shop.STATE_WAIT_AUTID);//设置状态未审核
        //获取当前注册的管理员信息
        Employee admin = shop.getAdmin();
        //添加进去 那么管理员的id就有了
        employeeMapper.add(admin);
        //将管理员的id设置到shop---AdminId管理员id
        shop.setAdminId(admin.getId());
        //保存店铺信息  店铺的id就有了
        shopMapper.add(shop);
        //将店铺的id 设置给管理员
        admin.setShopId(shop.getId());
    }
    @Override
    public void update(Shop shop) {
        //写什么东西


        super.update(shop);
    }

    @Override
    public List<ShopRo> countByDate() {
        return shopMapper.countByDate();
    }
    @Override
    public void activation(Long shopid) {
        //修改店铺的状态
        Shop one = shopMapper.findnoe(shopid);
        one.setState(BaseConstants.Shop.STATE_ACTIVE_SUCCESS);
        shopMapper.update(one);
        //得到管理员的id
        Long adminId = one.getAdminId();
        Employee one1 = employeeMapper.findnoe(adminId);
        one1.setState(BaseConstants.Employee.STATE_NORMAL);
        employeeMapper.update(one1);
    }
}
